package com.tonyodev.fetch2fileserver.database;


import ohos.data.rdb.AbsRdbPredicates;
import ohos.data.rdb.RawRdbPredicates;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;

import java.util.ArrayList;
import java.util.List;

public class FileResourceInfoDao_Impl implements FileResourceInfoDao{
    private String TAG = "sql " + FileResourceInfoDao_Impl.class.getSimpleName();
    private static RdbStore database;

    public FileResourceInfoDao_Impl(RdbStore database) {
        this.database = database;
    }

    public long insert(FileResourceInfo fileResourceInfo) {
        ValuesBucket valuesBucket =new ValuesBucket();
        valuesBucket.putLong(FileResourceInfo.COLUMN_ID, fileResourceInfo.getId());
        valuesBucket.putString(FileResourceInfo.COLUMN_NAME,fileResourceInfo.getName());
        valuesBucket.putString(FileResourceInfo.COLUMN_FILE,fileResourceInfo.getFile());
        valuesBucket.putString(FileResourceInfo.COLUMN_EXTRAS,fileResourceInfo.getExtras());
        valuesBucket.putLong(FileResourceInfo.COLUMN_LENGTH,fileResourceInfo.getLength());
        valuesBucket.putString(FileResourceInfo.COLUMN_MD5,fileResourceInfo.getMd5());
        if(database.insert(FileResourceInfo.TABLE_NAME,valuesBucket)>0){
            System.out.println(TAG+"成功");
            return database.insert(FileResourceInfo.TABLE_NAME,valuesBucket);
        }else{
            System.out.println(TAG+"失败");
            return 0;
        }
    }

    public List<Long> insert(List<FileResourceInfo> fileResourceInfos) {
        List<Long> longs=new ArrayList<>();
        ValuesBucket valuesBucket =new ValuesBucket();
        for(FileResourceInfo fileResourceInfo:fileResourceInfos){
            valuesBucket.putLong(FileResourceInfo.COLUMN_ID, fileResourceInfo.getId());
            valuesBucket.putString(FileResourceInfo.COLUMN_NAME,fileResourceInfo.getName());
            valuesBucket.putString(FileResourceInfo.COLUMN_FILE,fileResourceInfo.getFile());
            valuesBucket.putString(FileResourceInfo.COLUMN_EXTRAS,fileResourceInfo.getExtras());
            valuesBucket.putLong(FileResourceInfo.COLUMN_LENGTH,fileResourceInfo.getLength());
            valuesBucket.putString(FileResourceInfo.COLUMN_MD5,fileResourceInfo.getMd5());
            if(database.insert(FileResourceInfo.TABLE_NAME,valuesBucket)>0){
                longs.add(database.insert(FileResourceInfo.TABLE_NAME,valuesBucket));
            }else{
                System.out.println(TAG+"失败"+database.insert(FileResourceInfo.TABLE_NAME,valuesBucket));
            }
        }
        return longs;
    }

    public void delete(FileResourceInfo fileResourceInfo) {
        database.executeSql("DELETE FROM "+FileResourceInfo.TABLE_NAME+" WHERE "+FileResourceInfo.COLUMN_ID+" = "+fileResourceInfo.getId());
    }

    public void delete(List<FileResourceInfo> fileResourceInfoList) {
        for(FileResourceInfo fileResourceInfo:fileResourceInfoList){
            database.executeSql("DELETE FROM "+FileResourceInfo.TABLE_NAME+" WHERE "+FileResourceInfo.COLUMN_ID+" = "+fileResourceInfo.getId());
        }
    }

    public void deleteAll() {
        database.executeSql("DELETE FROM "+FileResourceInfo.TABLE_NAME+"");
    }
    public List<FileResourceInfo> get() {
        List<FileResourceInfo> parmFileResourceInfos=new ArrayList<>();
        String[] strings=new String[]{
            FileResourceInfo.COLUMN_ID, FileResourceInfo.COLUMN_LENGTH, FileResourceInfo.COLUMN_FILE, FileResourceInfo.COLUMN_NAME,
            FileResourceInfo.COLUMN_EXTRAS, FileResourceInfo.COLUMN_MD5
        };

        AbsRdbPredicates predicates=new RawRdbPredicates(FileResourceInfo.TABLE_NAME);
        ResultSet resultSet=database.query(predicates,strings);
        while (resultSet.goToNextRow()){
            long column_id = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_ID));
            long column_length = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_LENGTH));
            String column_file = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_FILE));
            String column_name = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_NAME));
            String column_extras = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_EXTRAS));
            String column_md5 = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_MD5));
            FileResourceInfo param=new FileResourceInfo(column_id,column_length,column_file,column_name,column_extras,column_md5);
            parmFileResourceInfos.add(param);
        }
        return parmFileResourceInfos;
    }

    public FileResourceInfo get(long id) {
        FileResourceInfo param=new FileResourceInfo();
        String[] strings=new String[]{
            FileResourceInfo.COLUMN_ID, FileResourceInfo.COLUMN_LENGTH, FileResourceInfo.COLUMN_FILE, FileResourceInfo.COLUMN_NAME,
            FileResourceInfo.COLUMN_EXTRAS, FileResourceInfo.COLUMN_MD5
        };
        ResultSet resultSet=database.query(new RawRdbPredicates(FileResourceInfo.TABLE_NAME),strings);
        while (resultSet.goToNextRow()){
            long column_id = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_ID));
            long column_length = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_LENGTH));
            String column_file = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_FILE));
            String column_name = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_NAME));
            String column_extras = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_EXTRAS));
            String column_md5 = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_MD5));
            if(column_id==id){
                param.setId(column_id);
                param.setLength(column_length);
                param.setFile(column_file);
                param.setName(column_name);
                param.setExtras(column_extras);
                param.setMd5(column_md5);
            }
        }
        return param;
    }

    public FileResourceInfo get(String fileName) {
        FileResourceInfo param=new FileResourceInfo();
        String[] strings=new String[]{
            FileResourceInfo.COLUMN_ID, FileResourceInfo.COLUMN_LENGTH, FileResourceInfo.COLUMN_FILE, FileResourceInfo.COLUMN_NAME,
            FileResourceInfo.COLUMN_EXTRAS, FileResourceInfo.COLUMN_MD5
        };
        AbsRdbPredicates absRdbPredicates=new RawRdbPredicates(FileResourceInfo.TABLE_NAME);
        ResultSet resultSet=database.query(absRdbPredicates,strings);
        while (resultSet.goToNextRow()){
            long column_id = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_ID));
            long column_length = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_LENGTH));
            String column_file = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_FILE));
            String column_name = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_NAME));
            String column_extras = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_EXTRAS));
            String column_md5 = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_MD5));
            if(column_name.equals(fileName)){
                param.setId(column_id);
                param.setLength(column_length);
                param.setFile(column_file);
                param.setName(column_name);
                param.setExtras(column_extras);
                param.setMd5(column_md5);
            }
        }
        return param;
    }

    public List<FileResourceInfo> get(List<Long> ids) {
        List<FileResourceInfo> parmFileResourceInfos=new ArrayList<>();
        StringBuffer sql =new StringBuffer();
        sql.append("SELECT * FROM "+FileResourceInfo.TABLE_NAME+" WHERE "+FileResourceInfo.COLUMN_ID+" IN (");
        for (int i = 0; i < ids.size(); i++) {
            sql.append("?");
            if (i < ids.size() - 1) {
                sql.append(",");
            }
        }
        sql.append(")");
        String[] strings=new String[]{
            FileResourceInfo.COLUMN_ID, FileResourceInfo.COLUMN_LENGTH, FileResourceInfo.COLUMN_FILE, FileResourceInfo.COLUMN_NAME,
            FileResourceInfo.COLUMN_EXTRAS, FileResourceInfo.COLUMN_MD5
        };
        ResultSet resultSet=database.querySql(sql.toString(),strings);
        while (resultSet.goToNextRow()){
            long column_id = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_ID));
            long column_length = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_LENGTH));
            String column_file = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_FILE));
            String column_name = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_NAME));
            String column_extras = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_EXTRAS));
            String column_md5 = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_MD5));
            FileResourceInfo param=new FileResourceInfo(column_id,column_length,column_file,column_name,column_extras,column_md5);
            parmFileResourceInfos.add(param);
        }
        return parmFileResourceInfos;
    }

    public List<FileResourceInfo> getPage(int count, int offset) {
        String sql ="SELECT * FROM "+FileResourceInfo.TABLE_NAME+" LIMIT  "+count+"  OFFSET " +offset;
        List<FileResourceInfo> parmFileResourceInfos=new ArrayList<>();
        String[] strings=new String[]{
            FileResourceInfo.COLUMN_ID, FileResourceInfo.COLUMN_LENGTH, FileResourceInfo.COLUMN_FILE, FileResourceInfo.COLUMN_NAME,
            FileResourceInfo.COLUMN_EXTRAS, FileResourceInfo.COLUMN_MD5
        };
        ResultSet resultSet=database.querySql(sql,strings);
        while (resultSet.goToNextRow()){
            long column_id = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_ID));
            long column_length = resultSet.getLong(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_LENGTH));
            String column_file = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_FILE));
            String column_name = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_NAME));
            String column_extras = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_EXTRAS));
            String column_md5 = resultSet.getString(resultSet.getColumnIndexForName(FileResourceInfo.COLUMN_MD5));
            FileResourceInfo param=new FileResourceInfo(column_id,column_length,column_file,column_name,column_extras,column_md5);
            parmFileResourceInfos.add(param);
        }
        return parmFileResourceInfos;
    }

}
